﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using appDataContracts;
using appInterfaces;
using System.ServiceModel;

namespace appClient.Account
{
	public partial class Login : System.Web.UI.Page
	{
		List<CaddieProduct> caddie;
		IClient client;
		public string s;

		protected void Page_Load(object sender, EventArgs e)
		{
			//RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);

			client = new ChannelFactory<IClient>("clientHttp").CreateChannel();
			if (Session["id"] != null)
				Response.Redirect("../Default.aspx");
		}

		protected void LoginButton_Click(object sender, EventArgs e)
		{
			connect();
		}

		public void connect()
		{
			// check if user exists
			SerializedClient sCli = client.checkAccount(UserName.Text.ToString(), Password.Text.ToString());

			if (sCli != null)
			{
				caddie = new List<CaddieProduct>();

				// creating a new session
				Session["user"] = sCli;

				/*Session["userID"] = 1; // A CHANGER
				Session["userEmail"] = "kikoo@alfinorekou.dad"; // A CHANGER
				Session["userFirstName"] = "Brotherhood"; // A CHANGER*/
				Session["caddie"] = caddie;

				Session.Timeout = 600;

				Response.Redirect("../Default.aspx");
			}
			else
			{
				FailureText.Text = "L'email et/ou le mot de passe est incorrect";
			}
		}
	}
}
